package com.miao.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miao.servicebase.exceptionhandler.EduException;
import com.miao.serviceedu.entity.EduTeacher;
import com.miao.serviceedu.entity.vo.EduTeacherQuery;
import com.miao.serviceedu.service.EduTeacherService;
import com.miao.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author miaohao
 * @since 2020-08-02
 */
@RestController
@RequestMapping("/serviceedu/edu-teacher")
@CrossOrigin
@Api(tags = {"讲师管理"})
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有讲师
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation(value = "所有讲师列表")
    public R findAll(){
        try {
            int a = 10/0;
        }catch(Exception e) {
            throw new EduException(20001,"出现自定义异常");
        }
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }



    @DeleteMapping("{id}")
    @ApiOperation(value = "删除讲师")
    public R deleteTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        eduTeacherService.removeById(id);
        return R.ok();
    }


    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        eduTeacherService.page(pageParam, null);

        return R.ok().data("rows", pageParam.getRecords()).data("total", pageParam.getTotal());
    }


    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                                  @RequestBody(required = false) EduTeacherQuery eduTeacherQuery){

        // 创建page对象
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        // 构建条件
        QueryWrapper queryWrapper = new QueryWrapper();
        // 讲师姓名
        String name = eduTeacherQuery.getName();
        // 头衔 1高级讲师 2首席讲师
        Integer level = eduTeacherQuery.getLevel();
        // 创建时间 start
        String begin = eduTeacherQuery.getBegin();
        // 创建时间 end
        String end = eduTeacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        // 查询
        eduTeacherService.page(pageParam, queryWrapper);
        // 返回
        return R.ok().data("rows", pageParam.getRecords()).data("total", pageParam.getTotal());
    }


    /**
     * 添加讲师接口
     * @param eduTeacher
     * @return
     */
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){

        boolean save = eduTeacherService.save(eduTeacher);

        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 根据ID查询讲师
     * @param id
     * @return
     */
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable String id){

        return R.ok().data("teacher", eduTeacherService.getById(id));
    }

    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

